#include "mywidget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSpacerItem>
#include <QDebug>

MyWidget::MyWidget(QWidget *parent)
    : FramelessWindow(parent)
{
    //this->setResizeable(false);
    InitForm();
    this->setTitleBarWidget({_titleWidget, _lblIcon, _lblTitle});
    this->setOnlyMoveByTitlebar(false);
    connect(_btnMin, &QPushButton::clicked, [this]() {
        this->setWindowState(this->windowState() & ~Qt::WindowActive | Qt::WindowMinimized);
    });
    connect(_btnMax, &QPushButton::clicked, [this]() {
        this->setWindowState(this->windowState().testFlag(Qt::WindowMaximized) ? Qt::WindowNoState : Qt::WindowMaximized);
    });
    connect(_btnClose, &QPushButton::clicked, [this]() { this->close(); });
}

MyWidget::~MyWidget()
{

}

void MyWidget::InitForm()
{
    // 1. 整体规划
    // 1.1 设置最小宽高
    this->setMinimumWidth(600);
    this->setMinimumHeight(400);

    // 1.3 窗体整体采用垂直布局，
    QVBoxLayout* vl = new QVBoxLayout(this);
    vl->setSpacing(0);
    vl->setContentsMargins(0, 0, 0, 0);

    // 1.4 并嵌套一层QWidget
    QWidget* frameWidget = new QWidget(this);
    vl->addWidget(frameWidget);

    // 1.5 嵌套的QWidget同样采用垂直布局
    QVBoxLayout* layout = new QVBoxLayout(frameWidget);
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);

    // 2. 标题栏
    // 2.1 设置固定高度、背景色
    _titleWidget = new QWidget(this);
    _titleWidget->setFixedHeight(48);
    _titleWidget->setStyleSheet("background-color: rgb(60, 60, 60)");

    // 2.2 标题栏采用水平布局
    QHBoxLayout* titleLayout = new QHBoxLayout(_titleWidget);
    titleLayout->setContentsMargins(8, 8, 8, 8);
    titleLayout->setSpacing(0);

    // 2.3 把标题栏添加到整体窗体布局中。之后就可以向titleLayout中添加控件了
    layout->addWidget(_titleWidget);

    // 2.4 添加图标
    _lblIcon = new QLabel(this);
    _lblIcon->setFixedSize(20, 20);
    _lblIcon->setStyleSheet("background-image: url(:/res/title_icon_20.svg); border: none");
    titleLayout->addWidget(_lblIcon);

    // 2.5 添加弹簧: 在图标和标题之间增加间距
    QSpacerItem* spacer = new QSpacerItem(6, 20, QSizePolicy::Fixed, QSizePolicy::Fixed);
    titleLayout->addSpacerItem(spacer);

    // 2.6 添加标题
    _lblTitle = new QLabel(this);
    _lblTitle->setText("完善的无边框窗口");
    _lblTitle->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    _lblTitle->setStyleSheet(R"(
        background-color: rgba(255, 255, 255, 0);
        font: 18px "Microsoft YaHei";
        color: rgb(200, 200, 200);
        border: none;
    )");
    titleLayout->addWidget(_lblTitle);

    // 2.7 添加最小化按钮
    _btnMin = new QPushButton(this);
    _btnMin->setCursor(QCursor(Qt::PointingHandCursor));
    _btnMin->setStyleSheet(R"(
        QPushButton {
            background-image:url(:/res/minimize_20.svg);
            border:none;
            background-repeat:none;
            background-position:center;
        }
        QPushButton:hover {
            background-color:rgb(86, 86, 86);
        }
    )");
    _btnMin->setFixedSize(36, 32);
    titleLayout->addWidget(_btnMin);

    // 2.8 添加最大化按钮
    _btnMax = new QPushButton(this);
    _btnMax->setCursor(QCursor(Qt::PointingHandCursor));
    _btnMax->setStyleSheet(R"(
        QPushButton {
            background-image:url(:/res/maximize_20_normal.svg);
            border:none;
            background-repeat:none;
            background-position:center;
        }
        QPushButton:hover {
            background-color:rgb(86, 86, 86);
        }
    )");
    _btnMax->setFixedSize(36, 32);
    titleLayout->addWidget(_btnMax);
    if (!this->getResizeable()) {
        _btnMax->hide();
    }

    // 2.9 添加关闭按钮
    _btnClose = new QPushButton(this);
    _btnClose->setCursor(QCursor(Qt::PointingHandCursor));
    _btnClose->setStyleSheet(R"(
        QPushButton {
            background-image:url(:/res/close_20.svg);
            border:none;
            background-repeat:none;
            background-position:center;
        }
        QPushButton:hover {
            background-color:rgb(222, 6, 26);
        }
    )");
    _btnClose->setFixedSize(36, 32);
    titleLayout->addWidget(_btnClose);

    // 3. 主体
    // 3.1 设置背景色
    _mainWidget = new QWidget(this);
    _mainWidget->setStyleSheet("background-color: #303030");

    // 3.2 主窗口采用水平布局
    QHBoxLayout* mainLayout = new QHBoxLayout(_mainWidget);
    mainLayout->setSpacing(10);
    mainLayout->setContentsMargins(10, 10, 10, 10);

    // 3.3 把主体添加到整体窗体布局中。之后就可以向mainLayout中添加控件了
    layout->addWidget(_mainWidget);

    // 3.4 添加确定和取消按钮
    QPushButton* btn1 = new QPushButton("确定", _mainWidget);
    QPushButton* btn2 = new QPushButton("取消", _mainWidget);

    mainLayout->addWidget(btn1);
    mainLayout->addWidget(btn2);

    QString style = R"(
        QPushButton {
            background-color: rgb(64, 64, 64);
            font:18px "Microsoft YaHei";
            color:rgb(200,200,200);
            border: 1px solid #707070;
            border-radius: 5px;
            padding: 5px;
        }
        QPushButton:hover {
            background-color: rgb(40, 40, 40);
        }
        QPushButton:pressed {
            background-color: rgb(64, 64, 64);
        }
     )";
    btn1->setStyleSheet(style);
    btn2->setStyleSheet(style);

    connect(btn1, &QPushButton::clicked, [=]() {
        qDebug() << "确定";
    });

    connect(btn2, &QPushButton::clicked, [=]() {
        qDebug() << "取消";
    });
}

void MyWidget::changeEvent(QEvent *event)
{
    if (event->type() == QEvent::WindowStateChange) {
        if (this->windowState().testFlag(Qt::WindowMaximized)) {
            _btnMax->setStyleSheet(R"(
                QPushButton {
                    background-image:url(:/res/maximize_20_max.svg);
                    border:none;
                    background-repeat:none;
                    background-position:center;
                }
                QPushButton:hover {
                    background-color:rgb(86, 86, 86);
                }
            )");
        } else if (this->windowState().testFlag(Qt::WindowNoState)) {
            _btnMax->setStyleSheet(R"(
                QPushButton {
                    background-image:url(:/res/maximize_20_normal.svg);
                    border:none;
                    background-repeat:none;
                    background-position:center;
                }
                QPushButton:hover {
                    background-color:rgb(86, 86, 86);
                }
            )");
        }
    }
}
